﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Helpers;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using LiteDB;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x020000E6 RID: 230
	public class CampaignTask : BindableBase
	{
		// Token: 0x17000156 RID: 342
		// (get) Token: 0x060004D1 RID: 1233 RVA: 0x00051AEA File Offset: 0x0004FCEA
		// (set) Token: 0x060004D2 RID: 1234 RVA: 0x00051AF4 File Offset: 0x0004FCF4
		protected int SLNum
		{
			[CompilerGenerated]
			get
			{
				return this.<SLNum>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<SLNum>k__BackingField == value)
				{
					return;
				}
				this.<SLNum>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.SLNum);
			}
		}

		// Token: 0x17000157 RID: 343
		// (get) Token: 0x060004D3 RID: 1235 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x17000158 RID: 344
		// (get) Token: 0x060004D4 RID: 1236 RVA: 0x00051B21 File Offset: 0x0004FD21
		// (set) Token: 0x060004D5 RID: 1237 RVA: 0x00051B2C File Offset: 0x0004FD2C
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x17000159 RID: 345
		// (get) Token: 0x060004D6 RID: 1238 RVA: 0x00051B59 File Offset: 0x0004FD59
		// (set) Token: 0x060004D7 RID: 1239 RVA: 0x00051B64 File Offset: 0x0004FD64
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x1700015A RID: 346
		// (get) Token: 0x060004D8 RID: 1240 RVA: 0x00051B95 File Offset: 0x0004FD95
		// (set) Token: 0x060004D9 RID: 1241 RVA: 0x00051BA0 File Offset: 0x0004FDA0
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x1700015B RID: 347
		// (get) Token: 0x060004DA RID: 1242 RVA: 0x00051BCD File Offset: 0x0004FDCD
		// (set) Token: 0x060004DB RID: 1243 RVA: 0x00051BD8 File Offset: 0x0004FDD8
		public int SLNumber
		{
			[CompilerGenerated]
			get
			{
				return this.<SLNumber>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<SLNumber>k__BackingField == value)
				{
					return;
				}
				this.<SLNumber>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.SLNumber);
			}
		}

		// Token: 0x1700015C RID: 348
		// (get) Token: 0x060004DC RID: 1244 RVA: 0x00051C05 File Offset: 0x0004FE05
		// (set) Token: 0x060004DD RID: 1245 RVA: 0x00051C10 File Offset: 0x0004FE10
		[BsonIgnore]
		public List<int> Fleet
		{
			[CompilerGenerated]
			get
			{
				return this.<Fleet>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<Fleet>k__BackingField, value))
				{
					return;
				}
				this.<Fleet>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Fleet);
			}
		}

		// Token: 0x1700015D RID: 349
		// (get) Token: 0x060004DE RID: 1246 RVA: 0x00051C40 File Offset: 0x0004FE40
		// (set) Token: 0x060004DF RID: 1247 RVA: 0x00051C48 File Offset: 0x0004FE48
		public int NodeIndex
		{
			[CompilerGenerated]
			get
			{
				return this.<NodeIndex>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<NodeIndex>k__BackingField == value)
				{
					return;
				}
				this.<NodeIndex>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.NodeID);
				this.OnPropertyChanged(<>PropertyChangedEventArgs.NodeIndex);
			}
		}

		// Token: 0x1700015E RID: 350
		// (get) Token: 0x060004E0 RID: 1248 RVA: 0x00051C80 File Offset: 0x0004FE80
		[BsonIgnore]
		public int NodeID
		{
			get
			{
				return (new int[]
				{
					101,
					102,
					201,
					202,
					301,
					302,
					401,
					402,
					501,
					502
				})[this.NodeIndex];
			}
		}

		// Token: 0x1700015F RID: 351
		// (get) Token: 0x060004E1 RID: 1249 RVA: 0x00051C9B File Offset: 0x0004FE9B
		// (set) Token: 0x060004E2 RID: 1250 RVA: 0x00051CA4 File Offset: 0x0004FEA4
		public int Formation
		{
			[CompilerGenerated]
			get
			{
				return this.<Formation>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Formation>k__BackingField == value)
				{
					return;
				}
				this.<Formation>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Formation);
			}
		}

		// Token: 0x17000160 RID: 352
		// (get) Token: 0x060004E3 RID: 1251 RVA: 0x00051CD1 File Offset: 0x0004FED1
		// (set) Token: 0x060004E4 RID: 1252 RVA: 0x00051CDC File Offset: 0x0004FEDC
		public bool IsNightWar
		{
			[CompilerGenerated]
			get
			{
				return this.<IsNightWar>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsNightWar>k__BackingField == value)
				{
					return;
				}
				this.<IsNightWar>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsNightWar);
			}
		}

		// Token: 0x17000161 RID: 353
		// (get) Token: 0x060004E5 RID: 1253 RVA: 0x00051D09 File Offset: 0x0004FF09
		// (set) Token: 0x060004E6 RID: 1254 RVA: 0x00051D14 File Offset: 0x0004FF14
		public int RepairStrategy
		{
			[CompilerGenerated]
			get
			{
				return this.<RepairStrategy>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<RepairStrategy>k__BackingField == value)
				{
					return;
				}
				this.<RepairStrategy>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.RepairStrategy);
			}
		}

		// Token: 0x17000162 RID: 354
		// (get) Token: 0x060004E7 RID: 1255 RVA: 0x00051D41 File Offset: 0x0004FF41
		// (set) Token: 0x060004E8 RID: 1256 RVA: 0x00051D4C File Offset: 0x0004FF4C
		public ObservableCollection<string> TacticsShip
		{
			get
			{
				return this._tacticsShip;
			}
			set
			{
				if (object.Equals(this._tacticsShip, value))
				{
					return;
				}
				this._tacticsShip = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.TacticsName);
				this.OnPropertyChanged(<>PropertyChangedEventArgs.TacticsShip);
				this.TacticsShip.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e)
				{
					base.RaisePropertyChanged("TacticsName");
				};
			}
		}

		// Token: 0x17000163 RID: 355
		// (get) Token: 0x060004E9 RID: 1257 RVA: 0x00051DA0 File Offset: 0x0004FFA0
		[BsonIgnore]
		public string TacticsName
		{
			get
			{
				if (this.TacticsShip.Count == 0)
				{
					return "点击设置";
				}
				return string.Join(",", from sid in this.TacticsShip
				select int.Parse(sid.Split(new char[]
				{
					'|'
				})[0]).ToName());
			}
		}

		// Token: 0x060004EA RID: 1258 RVA: 0x00051DF4 File Offset: 0x0004FFF4
		public async Task<bool> Prepare()
		{
			bool result;
			if (this.Game.User.campaignRemainNum == 0)
			{
				this.Fleet.Clear();
				result = false;
			}
			else
			{
				CampaignFleet campaignFleet = await Runtime.Remote.CampaignFleet(this.NodeID);
				this.Fleet = (from i in campaignFleet.campaignLevelFleet
				where i != 0
				select i).ToList<int>();
				if (this.Fleet.Count((int i) => i != 0) == 0)
				{
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]没有远征队伍"), "FullLog");
					result = false;
				}
				else if (this.Fleet.ToUserShipList().Any((UserShip i) => i.IsInExplore || i.IsInRepair))
				{
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]战役队伍中有船正远征或修理"), "FullLog");
					result = false;
				}
				else
				{
					List<UserShip> list = (from i in this.Fleet.ToUserShipList()
					where !i.IsInRepair && i.IsShipNeedRepair
					select i).ToList<UserShip>();
					if (list.Any<UserShip>())
					{
						if (this.RepairStrategy == 2)
						{
							foreach (UserShip item in list)
							{
								if (!this.Game.RepairTask.RepairQueue.Contains(item))
								{
									this.Game.RepairTask.RepairQueue.Enqueue(item);
								}
							}
							MessagingCenter.Send<LogMessage>(new LogMessage("[详细]战役队伍有船需要进入船坞修理"), "FullLog");
							return false;
						}
						if (this.RepairStrategy == 1)
						{
							list = (from i in list
							where i.BrokenType == ShipBrokenType.BigBroken
							select i).ToList<UserShip>();
						}
						await this.Game.InstantRepairShip((from i in list
						select i.id).ToList<int>());
					}
					if (this.Fleet.ToUserShipList().Any((UserShip i) => i.IsInRepair || i.BrokenType == ShipBrokenType.BigBroken))
					{
						MessagingCenter.Send<LogMessage>(new LogMessage("[详细]战役队伍有船正在修理或大破"), "FullLog");
						result = false;
					}
					else
					{
						result = true;
					}
				}
			}
			return result;
		}

		// Token: 0x060004EB RID: 1259 RVA: 0x00051E3C File Offset: 0x0005003C
		public async Task Start()
		{
			if (!this.Fleet.ToUserShipList().Any((UserShip i) => i.IsInRepair || i.BrokenType == ShipBrokenType.BigBroken))
			{
				MessagingCenter.Send<LogMessage>(new LogMessage("[战役]开始战役"), "Log");
				MessagingCenter.Send<LogMessage>(new LogMessage("[详细]补给队伍"), "FullLog");
				await this.Game.SupplyFleet(this.Fleet.ToArray(), 0);
				await this.Game.Delay(500);
				MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开始索敌"), "FullLog");
				FoundEnemyVO spy = await Runtime.Remote.CampaignSpy(this.NodeID);
				await this.Game.Delay(1000);
				List<ShipType> list = (from i in spy.enemyShips
				select i.type).ToList<ShipType>();
				MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开打..."), "FullLog");
				DealNode deal;
				if (this.Game.GlobalConfig.AutoOneColume && list.Contains(ShipType.Submarine))
				{
					deal = await Runtime.Remote.CampaignDeal(this.NodeID, AutoR.Models.Formation.OneColume);
				}
				else
				{
					deal = await Runtime.Remote.CampaignDeal(this.NodeID, this.Formation + AutoR.Models.Formation.OneRow);
				}
				if (this.Game.GlobalConfig.CampaignSL)
				{
					if (this.SLNumber < 0)
					{
						this.SLNumber = 0;
						this.Game.GlobalConfig.CampaignSL = false;
						MessagingCenter.Send<LogMessage>(new LogMessage("[战役]SL次数达到设置值,停止SL"), "Log");
					}
					else
					{
						using (List<int>.Enumerator enumerator = this.Fleet.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								int us = enumerator.Current;
								UserShip userShip = this.Game.UserShip.Single((UserShip i) => i.id == us);
								userShip.battleProps.oil = 0;
								userShip.battleProps.ammo = 0;
							}
						}
						await this.Game.Delay(new Random().Next(3000, 10000));
						await this.Game.GoHome();
						await this.Game.Delay(2000);
						if (this.TacticsShip.Any<string>())
						{
							if ((from i in this.Game.Tactics
							where this.TacticsShip.Any((string e) => int.Parse(e.Split(new char[]
							{
								'|'
							})[0]) == i.boat_id && int.Parse(e.Split(new char[]
							{
								'|'
							})[1]) == i.tactics_id)
							select i).Any((Tactic i) => i.IsFullLevel))
							{
								MessagingCenter.Send<LogMessage>(new LogMessage("[战役]学院满级,停止SL"), "Log");
								MessagingCenter.Send<LogMessage>(new LogMessage("学院满级,停止SL"), "Notification");
								this.Enabled = false;
								this.Game.GlobalConfig.CampaignSL = false;
								return;
							}
						}
						if (this.SLNumber > 0)
						{
							this.SLNumber--;
						}
						MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[战役]进入战斗，第{0}次重启", this.SLNum)), "Log");
						this.SLNum++;
					}
				}
				else
				{
					await this.Game.Delay(new Random().Next(this.Game.GlobalConfig.MinSeconds * 1000, this.Game.GlobalConfig.MaxSeconds * 1000));
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]获取战斗结果"), "FullLog");
					BattleResult battleResult = await Runtime.Remote.CampaignResult(this.IsNightWar && deal.warReport.canDoNightWar);
					this.Game.UserShip.UpdateUserShip(battleResult.shipVO);
					this.Game.Package.UpdateUserPackage(battleResult.packageVo);
					MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[战役]完成一次战役，结果:【{0}】", battleResult.warResult.resultLevel)), "Log");
					this.Game.User.campaignRemainNum--;
				}
			}
		}

		// Token: 0x060004EC RID: 1260 RVA: 0x00051E81 File Offset: 0x00050081
		public CampaignTask()
		{
			this.<SLNum>k__BackingField = 1;
			this.<Id>k__BackingField = 4;
			this.<Type>k__BackingField = "CampaignTask";
			this.<Fleet>k__BackingField = new List<int>();
			this._tacticsShip = new ObservableCollection<string>();
			base..ctor();
		}

		// Token: 0x040003C3 RID: 963
		private ObservableCollection<string> _tacticsShip;
	}
}
